import { GetLoginPersonName, GetTaskTime, GetCheckLevel, GetCheckList } from '../../../services/cq_problem_entr.js';
import { GetCheckProject, GetCheckItem, PostProblem } from '../../../services/cq_problem_entr.js';
import { GetCompany } from '../../../services/gettreedata.js';
import { GetDictionary } from '../../../services/dictionary';
import { GetCurrentUser } from '../../../store/CurrentUser';
import { GetEmployees } from '../../../services/cq_problem_entr';
// import { GetEmployees } from '../../../services/employee';

export default {
    data() {
        return {
            formData: {
                checkStaffID:null,
                checkStaffName: "",
                // checkType:"", // 检查类型
                // taskDate: "", // 不传，起止时间结合起来 
                checkLevel: "",
                checkDate: "",
                companyCode: "",
                // companyCodeName: "", // 不传，后端查询
                // parentCompanyName:"", // 不传，后端查询
                address: "",
                checkListName: "",    
                checkListID: null,    
                checkItem: "",
                // checkItemCode: "",    // 不传
                checkListItemName: "",
                checkListItemCode: "",
                problemDescription: "",
                problemType: "",
                problemRank: "",
                problemSource: "",
                problemGist: "",
                hseFactor: "",
                responsePersonID: null,
                // responsePersonName: "", // 不传，后端查
                employmentProperty: "",
                workingAge:"",
                workingName:"",
                supervisorID: null ,
                // SupervisorName: "", // 不传，后端查
                checkAttach1: "" ,
                checkAttach2: "" ,
                checkAttach3: "" ,
                checkAttach4: "" ,
                checkVideo: "" ,
                immediately: "否",
                lostScore: "",
                penalty: "",
                education: "否",
                rectification: "",
                rectAttach1: "" ,
                rectAttach2: "" ,
                rectAttach3: "" ,
                rectAttach4: "" ,
                rectVideo: "" ,
                verifyPersonEmpID: null,
                verifyPersonName: "",
                verifyDate: "",
                rectEffect:"",
            },
            checkTypeOptions:[],
            checkVideoFlag:false,
            checkVideoUploadPercent:0,
            checkVideourl:"",
            rectVideoFlag:false,
            rectVideoUploadPercent:0,
            rectVideourl:"",
            checkRankOptions: [],
            checkCompanyNameOptions: [],
            checkCompanyList: [],
            checkListOptions: [],
            checkItemOptions: [],
            checkItemDetailOptions: [],
            problemTypeOptions:[],
            problemRankOptions:[],
            problemSourceOptions: [],
            responsePersonOptions:[],
            supervisorOptions:[],
            dialogImageUrl: '',
            dialogVisible: false,
            disabled: false,
            immediateRectification: false,
            criticalEducation:false,
            scoreOptions:[],
            moneyOptions:[],
            rectificationOptions:[],
            rectificationEffectOptions:[],
            workingAgeOptions:[],
        }
    },
    computed:{
        currentUser(){
            return GetCurrentUser()
        },
    },
    mounted () {
        this.handleGetLoginPersonName();
        this.handleGetEmployees();
        this.handleGetTaskTime();
        this.handleGetCheckLevel();
        this.handleGetCompany();
        this.handleGetCheckList();
        this.getDictionaryCheckType();
        this.getDictionaryProblemType();
        this.getDictionaryProblemRank();
        this.getDictionaryProblemSource();
        this.getDictionaryWorkingAge();
        this.getDictionaryLostScore();
        this.getDictionaryPenalty();
        this.getDictionaryRectification();
        this.getDictionaryRectEffect();
    },
    methods: {
        handleGetLoginPersonName(){
            GetLoginPersonName().then((res) =>{
                this.formData.checkStaffName = res.data.name;
                this.formData.checkStaffID = res.data.employeeID;
            }).catch((err) => {
                this.$message.error(err.message);
            });
        },
        handleGetEmployees (val='') {
            GetEmployees({name: val}).then((res) => {
                this.responsePersonOptions = res.data
                this.supervisorOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
                this.leaderLoading = false
            })
        },
        handleGetTaskTime(){
            GetTaskTime().then((res) => {
                // console.log(res.data.hasOwnProperty('endDate'))
                if (typeof res.data.endDate === "string"){
                    this.formData.taskDate = res.data.startDate + "~" + res.data.endDate;
                } else {
                    this.formData.taskDate = "暂无任务时间";
                }
            }).catch((err) => {
                this.$message.error(err.message);
            });
        },
        handleGetCheckLevel(){
            GetCheckLevel().then((res) => {
                res.data.forEach(element => {
                    this.checkRankOptions.push({value:element, label:element});
                });
            }).catch((err)  => {
                this.$message.error(err.message);
            });
        },
        handleGetCompany(){
            GetCompany().then((res) => {
                this.checkCompanyNameOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message);
            });
        },
        handleGetCheckList(){
            GetCheckList().then((res) => {
                if (res.data.length > 0){
                    this.checkListOptions = res.data;
                }
            }).catch((err) => {
                this.$message.error(err.message);
            });
        },
        changeCheckList(selectValue){
            this.formData.checkItem = "" ;
            this.formData.checkItemCode = "";
            this.formData.checkListItemName = "" ;
            this.formData.checkListItemCode = "";
            this.formData.problemDescription = "";
            this.formData.problemRank = "";
            this.formData.problemGist = "";
            this.checkListOptions.forEach(element => {
                if (element.checkListID === selectValue) {
                    this.formData.checkListName = element.checkListName;
                }
            });
            if(selectValue){
                GetCheckProject(selectValue).then((res) => {
                    this.checkItemOptions = res.data;
                }).catch((err) => {
                    this.$message.error(err.message);
                })
            } else {
                this.checkItemOptions = [];
                this.checkItemDetailOptions = [];
            }
        },
        changeCheckProject(selectValue){
            this.formData.checkListItemName = "" ;
            this.formData.checkListItemCode = "";
            this.formData.problemDescription = "";
            this.formData.problemRank = "";
            this.formData.problemGist = "";
            this.checkItemOptions.forEach(element => {
                if (element.checkListItemCode === selectValue) {
                    this.formData.checkItem = element.checkListItemName;
                }
            });
            if(selectValue){
                const selectlist = {projectCode:selectValue,listID:this.formData.checkListID}
                GetCheckItem(selectlist).then((res) => {
                    this.checkItemDetailOptions = res.data;
                }).catch((err) => {
                    this.$message.error(err.message);
                })
            } else {
                this.checkItemDetailOptions = [];
            }
        },
        changeCheckItemDetail(selectValue){
            this.formData.problemDescription = "";
            this.formData.problemRank = "";
            this.formData.problemGist = "";
            this.checkItemDetailOptions.forEach(element =>{
                if (element.checkListItemCode === selectValue) {
                    this.formData.checkListItemName = element.checkListItemName;
                    this.formData.problemDescription = element.checkListItemName;
                    this.formData.problemRank = element.problemRank;
                    this.formData.problemGist = element.gist;
                }
            });
        },
        changeResponsibilityPerson(selectValue){
            this.formData.employmentProperty = "";
            this.formData.workingName = "";
            this.responsePersonOptions.forEach(element => {
                if (element.employeeID === selectValue){
                    this.formData.employmentProperty = element.empGroup;
                    this.formData.workingName = element.station;
                }
            });
        },
        getDictionaryCheckType(){
            GetDictionary({name: '检查类型'}).then((res) => {
                this.checkTypeOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        getDictionaryProblemType(){
            GetDictionary({name: '问题类型'}).then((res) => {
                this.problemTypeOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        getDictionaryProblemRank(){
            GetDictionary({name: '隐患等级'}).then((res) => {
                this.problemRankOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        getDictionaryProblemSource(){
            GetDictionary({name: '问题原因'}).then((res) => {
                this.problemSourceOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        getDictionaryWorkingAge(){
            GetDictionary({name: '岗位工龄'}).then((res) => {
                this.workingAgeOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        getDictionaryLostScore(){
            GetDictionary({name: '扣分'}).then((res) => {
                this.scoreOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        getDictionaryPenalty(){
            GetDictionary({name: '扣款'}).then((res) => {
                this.moneyOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        
        getDictionaryRectification(){
            GetDictionary({name: '整改情况'}).then((res) => {
                this.rectificationOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        getDictionaryRectEffect(){
            GetDictionary({name: '整改结果'}).then((res) => {
                this.rectificationEffectOptions = res.data
            }).catch((err) => {
                this.$message.error(err.message)
            })
        },
        changeImmediateRectification(value){
            if (value === false){
                this.formData.immediately = "否"
                this.formData.lostScore = null;
                this.formData.penalty = null;
                this.formData.rectification = "";
                this.formData.education = "否";
                this.formData.rectAttach1 = "";
                this.formData.rectAttach2 = "";
                this.formData.rectAttach3 = "";
                this.formData.rectAttach4 = "";
                this.formData.rectVideo = "";
                this.formData.verifyPersonEmpID = null;
                this.formData.verifyPersonName = "";
                this.formData.verifyDate = "";
                this.formData.rectEffect = "";
            } else {
                this.formData.verifyPersonName = this.formData.checkStaffName;
                this.formData.verifyPersonEmpID = this.formData.checkStaffID;
                this.formData.immediately = "是"
            }
        },
        changeCriticalEducation(value){
            if (value === true){
                this.formData.education = "是";
            }
        },
        handleBeforeUpload(file){
            let size = file.size / 1024 / 1024 / 12
            if(size > 1) {
                this.$message.error('上传视频大小不能超过12MB!');
                return false;
            }
        },
        handleRemoveCheckAttach1() {
            this.formData.checkAttach1 = "";
        },
        handleSuccessCheckAttach1(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.checkAttach1 = response.data
            }
        },
        handleRemoveCheckAttach2() {
            this.formData.checkAttach2 = "";
        },
        handleSuccessCheckAttach2(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.checkAttach2 = response.data
            }
        },
        handleRemoveCheckAttach3() {
            this.formData.checkAttach3 = "";
        },
        handleSuccessCheckAttach3(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.checkAttach3 = response.data
            }
        },
        handleRemoveCheckAttach4() {
            this.formData.checkAttach4 = "";
        },
        handleSuccessCheckAttach4(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.checkAttach4 = response.data
            }
        },
        uploadCheckVideoProcess(event, file){
            this.checkVideoFlag = true;
            this.checkVideoUploadPercent = parseInt(file.percentage);
        },
        handleRemoveCheckVideo() {
            this.formData.checkVideo = "";
        },
        handleSuccessCheckVideo(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.checkVideo = response.data
                this.checkVideoFlag = false;
                this.checkVideoUploadPercent = 0;
                this.checkVideourl='http://39.98.173.131:8080/resources/'+ this.formData.checkVideo
            }
        },
        handleRemoveRectAttach1() {
            this.formData.rectAttach1 = "";
        },
        handleSuccessRectAttach1(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.rectAttach1 = response.data
            }
        },
        handleRemoveRectAttach2() {
            this.formData.rectAttach2 = "";
        },
        handleSuccessRectAttach2(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.rectAttach2 = response.data
            }
        },
        handleRemoveRectAttach3() {
            this.formData.rectAttach3 = "";
        },
        handleSuccessRectAttach3(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.rectAttach3 = response.data
            }
        },
        handleRemoveRectAttach4() {
            this.formData.rectAttach4 = "";
        },
        handleSuccessRectAttach4(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.rectAttach4 = response.data
            }
        },
        uploadRectVideoProcess(event, file){
            this.rectVideoFlag = true;
            this.rectVideoUploadPercent = parseInt(file.percentage);
        },
        handleRemoveRectVideo() {
            this.formData.rectVideo = "";
        },
        handleSuccessRectVideo(response){
            if(response) {
                this.$notify.success({
                    body:{
                        style:{width:'300px',height:'500px'}
                    },
                    title:'提示',
                    message: '文件上传成功'
                })
                this.formData.rectVideo = response.data
                this.rectVideoFlag = false;
                this.rectVideoUploadPercent = 0;
                this.rectVideourl='http://39.98.173.131:8080/resources/'+ this.formData.rectVideo
            }
        },
        handleSubmit(){
            console.log(JSON.stringify(1));
            this.formData.companyCode = this.checkCompanyList[this.checkCompanyList.length-1];
            // console.log(JSON.stringify(this.formData));
            console.log(this.formData.checkAttach1)
            PostProblem(this.formData).then((res) => {
                this.$message({
                    message: res.message,
                    type: 'success'
                });
                // this.$router.go(0);
            }).catch((err) => {
                this.$message.error(err.message);
            })
        }
    }
}